package com.jiamao.goplay.ui.login;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.LoginRegiestUserinfoAddHeadImageLayoutBinding;
import com.jiamao.goplay.ui.login.vm.AddHeadImageModel;
import com.jiamao.goplay.ui.photo.PhotoUploadFragment;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.List;

import me.goldze.mvvmhabit.utils.KLog;

/**
 * 添加头像
 */
public class AddHeadImageFragment extends BaseMVVMFragment<LoginRegiestUserinfoAddHeadImageLayoutBinding, AddHeadImageModel> {

    private String image,nickName,birthday,constellations,sex;




    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.login_regiest_userinfo_add_head_image_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public AddHeadImageModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(AddHeadImageModel.class);
    }


    @Override
    public void initData() {
        super.initData();

          Bundle bd  = getArguments();
          nickName = bd.getString("nickName");
        birthday = bd.getString("birthday");
        constellations = bd.getString("constellations");
        sex = bd.getString("sex");

        viewModel.nickName.set(nickName);
        viewModel.birthday.set(birthday);
        viewModel.constellations.set(constellations);
        viewModel.sex.set(sex);

        binding.tvNickNameTitle.setText(nickName);
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();

        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("");
        binding.getRoot().findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });


        viewModel.uc.showTime.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showPhoto();
            }
        });

        viewModel.uc.image.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String String) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                image = String;
                viewModel.image.set(image);

                KLog.d("image  "+image);

                Glide.with(getContext()).load(String).placeholder(me.goldze.mvvmhabit.R.drawable.icon_img_loading)
                        .error( me.goldze.mvvmhabit.R.drawable.icon_img_loading_error).into(  binding.imgUserPhoto);

                binding.imgUserPhotoTip.setVisibility(View.GONE);
                binding.tvNext.setEnabled(true);
                binding.tvNext.setBackgroundResource(R.drawable.bg_login_btn_ok);
                binding.tvNext.setTextColor(Color.WHITE);

//                Glide.with(getContext()).asBitmap().load(image).placeholder(me.goldze.mvvmhabit.R.drawable.icon_img_loading)
//                        .error( me.goldze.mvvmhabit.R.drawable.icon_img_loading_error).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
//                    @Override
//                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                        int imageWidth = resource.getWidth();
//                        int imageHeight = resource.getHeight();
//
//                        double sc  = (Double.valueOf(imageWidth)/+Double.valueOf(imageHeight));
//                        KLog.d("imageWidth:"+imageWidth+" imageHeight:"+imageHeight+"   mx:"+sc);
//                        if (sc< 0.75){
//                            int max =  binding.imgUserPhoto.getWidth();
//                            int height =  max * imageHeight / imageWidth;
//                            ViewGroup.LayoutParams para = binding.imgUserPhoto.getLayoutParams();
//                            para.height = height;
//                            para.width = max;
//                        }
//                        binding.imgUserPhoto.setImageBitmap(resource);
//                    }
//
//                });
            }
        });


//
//        bundle.putString("nickName",nickName.get());
//        bundle.putString("birthday",birthday.get());
//        bundle.putString("constellations",constellations.get());
//        bundle.putString("sex",sex.get());

    }

    public void showPhoto(){
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.GRAY)
                // “确定”按钮文字颜色
                .btnTextColor(Color.BLUE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.BLACK)
                // 返回图标ResId
                .backResId(R.mipmap.icon_back)
                // 标题
                .title("图片")
                // 标题文字颜色
                .titleColor(Color.BLACK)
                // TitleBar背景色
                .titleBgColor(Color.WHITE)
                // 裁剪大小。needCrop为true的时候配置
                .cropSize(1, 1, 200, 200)
                .needCrop(false)
                // 第一个是否显示相机，默认true
                .needCamera(false)
                // 最大选择图片数量，默认9
                .maxNum(1)
                .build();
// 跳转到图片选择器
        ISNav.getInstance().toListActivity(this, config, 1);



    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode ==  1  && data != null) {
            List<String> pathList = data.getStringArrayListExtra("result");


            Bundle bd = new Bundle();
            bd.putString("img",pathList.get(0));
            bd.putInt("from",1);
            bd.putInt("type",3);
            startContainerActivity(PhotoUploadFragment.class.getCanonicalName(),bd);
          //  viewModel.uploadImage(pathList.get(0));

        }
    }


}
